# -*- coding: utf-8 -*-
import pymysql
import os
import shutil

# 连接到MySQL数据库
connection = pymysql.connect(host='127.0.0.1', user='root', password='Gztt@0851', database='gz_erp_db', port=3336)

# 查询pbi_attach表并获取所需字段
#1 0=合同2=保险 1 3=安全责任书 2 4=岗前教育培训 3 5=项目启动前的安全技术交底 46=服务订单工单 5 7=任务单派发 6
sql = """SELECT task_attach_url7, service_attach_url6, insure_attach_url2, safety_attach_url3, 
                 train_attach_url4, technical_attach_url5, project_attach_url1
          FROM pbi_person_code"""

with connection.cursor() as cursor:
    cursor.execute(sql)
    results = cursor.fetchall()

# 关闭数据库连接
connection.close()

# 定义源目录和目标目录
source_dir = "/usr/local/file/rds/temp/"
destination_dir = "/usr/local/file/rds/opoc_url/"

# 遍历查询结果，将源目录中的文件复制到目标目录
for row in results:
    for attach_url in row:
        if attach_url:
            file_name = os.path.basename(attach_url)
            source_file_path = os.path.join(source_dir, file_name)
            destination_file_path = os.path.join(destination_dir, file_name)

            if os.path.isfile(source_file_path):
                shutil.copy2(source_file_path, destination_file_path)
                print("File '{}' copied successfully.".format(file_name))
            else:
                print("File '{}' not found in source directory.".format(file_name))
